<template>
  <van-picker
    title="性别"
    show-toolbar
    :default-index="value"
    :columns="columns"
    @confirm="emitSex"
  />
</template>

<script>
import { emitUserFile } from "@/api/user.js";

export default {
  props: {
    value: {
      type: [String, Number],
      required: true,
    },
  },

  data() {
    return {
      columns: ["男", "女"],
    };
  },
  methods: {
    async emitSex(val) {
      if (this.columns[this.value] === val) return this.$emit("delUserShow");
      // 加载
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        await emitUserFile({
          gender: val === "男" ? 0 : 1,
        });
        this.$toast.success("更新成功");
        this.$bus2.$emit("delUserSex", true);
      } catch (error) {
        this.$toast("更新失败");
      }
    },
  },
};
</script>

<style>
</style>